package com.study.spmongodb.book.wwj.char01;

import java.util.concurrent.TimeUnit;

/**
 * @author MI
 * @version 1.0
 * @date 2020/11/23 22:09
 */
public class TicketWindowRunnable implements Runnable {
    private int index = 1;//不做static修饰
    private final static int MAX = 500;
    private final static Object MUTEX = new Object();

    @Override
    public void run() {
        synchronized (MUTEX) {
            while (index <= MAX) {
                System.out.println(Thread.currentThread().getName() + " 的号码是 " + (index++));
                try {
                    TimeUnit.MILLISECONDS.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        final TicketWindowRunnable task = new TicketWindowRunnable();
        final Thread w1 = new Thread(task, "一号窗口");
        final Thread w2 = new Thread(task, "二号窗口");
        final Thread w3 = new Thread(task, "三号窗口");
        final Thread w4 = new Thread(task, "四号窗口");
        w1.start();
        w2.start();
        w3.start();
        w4.start();
    }
}
